import numpy as np
from sklearn.preprocessing import PolynomialFeatures
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression

# 数据归一化处理
def processing(data):
    scaler = StandardScaler()
    data_scaled = scaler.fit_transform(data)
    return scaler, data_scaled

# 假设我们有以下两个特征的数据集
data = np.array(
    [[100, 32],
     [90, 23],
     [150, 45],
     [60, 40],
     [80, 42],
     [150, 30]])

# 贷款金额
labels = np.array([20, 24, 40, 15, 10, 17])

# 分割数据集，前5个用于训练，最后一个用于测试
data_train = data[:5]
data_test = data[5:]
labels_train = labels[:5]
labels_test = labels[5:]

# 数据归一化
scaler, data_scaled_train = processing(data_train)
print(data_scaled_train)

# 创建PolynomialFeatures实例，degree=2
poly_features = PolynomialFeatures(degree=2, include_bias=False)

# 转换训练特征
X_poly_train = poly_features.fit_transform(data_scaled_train)

# 使用线性回归模型
model = LinearRegression()
model.fit(X_poly_train, labels_train)

# 归一化测试数据
data_scaled_test = scaler.transform(np.array([[150, 31]]))  # 使用训练集的尺度参数

# 转换测试特征
X_poly_test = poly_features.transform(data_scaled_test)

# 使用模型进行预测
predictions = model.predict(X_poly_test)

# 打印测试数据的预测结果
print("测试数据的预测结果:", predictions)

# 获取参数值（系数）
coefficients = model.coef_

# 获取截距项
intercept = model.intercept_

print("模型参数值（系数）:", coefficients)
print("模型截距项:", intercept)
